<?php
/**
 * 属性管理
 * @author Mortimer He <mortimerhe@163.com>
 */
namespace Manage\Controller;
use Common\Library\Page;
class AttributeController extends CommonController{
	private $msg=array();
	private $Model;
	function _initialize(){
		parent::_initialize();
		$this->Model=D('Attribute');
	}
	public function index(){
		if(!$this->checkPermission('Attribute')) $this->jump();
		$count=$this->Model->getCount();
		$Page=new Page($count,C('CONFIG_LIMIT_MANAGE'));
		$data['start']=$Page->firstRow;
		$data['limit']=$Page->listRows;
		$Page->setConfig('header',L('page_header'));
		$Page->setConfig('theme',L('page_theme'));
		$list=$this->Model->getAttributes($data);
		foreach ($list as $k => $v) {
			$attribute_group=D('AttributeGroup')->getById($v['attribute_group_id']);
			$list[$k]['attribute_group']=$attribute_group['attribute_group_description'][get_default_lng_id()]['name'];
		}
		$show=$Page->show();
		$url['p']=I('get.p','');
		$this->assign('url',$url);
		$this->assign('page',$show);
		$this->assign('list',$list);
		$this->display();
	}
	public function oper($attribute_id=0){
		if(!$attribute_id && !$this->checkPermission('AddAttribute')) $this->jump();
		$attribute=$this->Model->getById($attribute_id);
		if($attribute_id && empty($attribute)) $this->jump(L('error_404'));
		if(IS_POST){
			$attribute=I('post.');
			if($this->validateForm($attribute_id)){
				$this->Model->operAttribute($attribute,$attribute_id);
				session('success',$attribute_id?L('success_edit'):L('success_add'));
				$url['p']=I('get.p','');
				$this->redirect('index',$url);
			}else{
				if(!isset($this->msg['warning'])) $this->msg['warning']=L('error_warning');
			}
		}
		$attribute_groups=D('AttributeGroup')->getAll();
		$this->assign('attribute_groups',$attribute_groups);
		$this->assign('attribute',$attribute);
		$this->assign('msg',$this->msg);
		$this->assign('languages',get_langs());
		$this->display();
	}
	public function del(){
		if($this->checkPermission('DeleteAttribute')){
			if($this->validateDelete()){
				$ids=I('post.selected');
				$count=0;
				foreach ($ids as $attribute_id) {
					$count += $this->Model->del($attribute_id);
				}
				session('success',L('success_delete',array('count'=>$count)));
			}else{
				session('danger',L('error_delete'));
			}
			
		}else{
			session('danger',L('error_not_permission'));
		}
		$this->redirect('index');

	}
	private function validateForm($attribute_id){
		if($attribute_id && !$this->checkPermission('EditAttribute')){
			$this->msg['warning']=L('error_not_permission');
			return !$this->msg;
		}
		$data=I('post.');
		foreach ($data['attribute_description'] as $k => $v) {
			if(mb_strlen($v['name'])>64 || mb_strlen($v['name'])<2){
				$this->msg['name'][$k]=L('error_name');
			}
		}
		return !$this->msg;
	}
	private function validateDelete(){
		return true;
	}

	public function autocomplete($filter_name=''){
		$data=array(
			'start'		=>	0,
			'limit'		=>	8,
			'name'		=>	$filter_name
			);
		$attributes=$this->Model->getAttributes($data);
		$sort_order=array();
		foreach ($attributes as $k => $v) {
			$attribute_group=D('AttributeGroup')->getById($v['attribute_group_id']);
			$attributes[$k]['attribute_group']=$attribute_group['attribute_group_description'][get_default_lng_id()]['name'];
			$sort_order[$k]=$v['name'];
		}
		array_multisort($sort_order, SORT_ASC, $json);
		$this->ajaxReturn($attributes,'JSON');
	}
}